1 00:00:00,260 --> 00:00:01,250 Welcome back. 2 00:00:01,250 --> 00:00:06,680 In this lecture we're going to be learning a crucial service for scripting any kind of system relating 3 00:00:06,710 --> 00:00:08,120 to user input. 4 00:00:08,120 --> 00:00:10,880 This service is called the User Input Service. 5 00:00:10,880 --> 00:00:16,190 And it contains many functions and events that allow us to listen to a user's input on their local device. 6 00:00:16,190 --> 00:00:21,200 And since this service is purely for user input, it will only work within a local script. 7 00:00:21,200 --> 00:00:26,540 So to get access to this service, we can create a variable and I'm going to call it User Input Service. 8 00:00:26,810 --> 00:00:32,030 And we can go ahead and use the Get Service function and grab the user input service. 9 00:00:32,150 --> 00:00:37,880 And if we index the user input service, there is a plethora of different functions and properties and 10 00:00:37,880 --> 00:00:40,340 events, all relating to user input. 11 00:00:40,340 --> 00:00:45,710 For example, there's many Get functions that allow us to get things such as the mouse's location on 12 00:00:45,710 --> 00:00:47,600 the screen or the mouse delta. 13 00:00:47,600 --> 00:00:49,670 We can get the gamepad state. 14 00:00:49,670 --> 00:00:51,980 We can get current keys that are pressed down. 15 00:00:51,980 --> 00:00:57,230 We can check to see whether or not the player is focused inside of a text box on their screen, and 16 00:00:57,230 --> 00:00:59,000 a bunch of other neat functions. 17 00:00:59,000 --> 00:01:01,700 And of course, we also have a bunch of events in here. 18 00:01:01,700 --> 00:01:06,020 So for example, we can listen to when an input begins or an input ends. 19 00:01:06,020 --> 00:01:12,260 We can listen for touches on the screen or a tap in the world from a mobile device and things of that 20 00:01:12,260 --> 00:01:12,890 nature. 21 00:01:12,890 --> 00:01:17,450 The most important events that we're going to be taking a look at is going to be input began and input 22 00:01:17,450 --> 00:01:17,870 ended. 23 00:01:17,870 --> 00:01:20,300 So I'm going to index for input began. 24 00:01:20,300 --> 00:01:23,750 And then we can go ahead and connect a function to this event. 25 00:01:23,750 --> 00:01:27,320 And as you can see this function gets passed two different arguments. 26 00:01:27,320 --> 00:01:30,320 One is an input object and the other is a boolean. 27 00:01:30,320 --> 00:01:35,660 And this boolean lets us know whether or not this input was observed within a UI element. 28 00:01:35,660 --> 00:01:41,390 So for example, if a player touched a button on their screen, then this game processed event boolean 29 00:01:41,390 --> 00:01:47,480 would return true if a player just tapped randomly on their screen without touching any UI, then this 30 00:01:47,480 --> 00:01:49,100 boolean would be false. 31 00:01:49,640 --> 00:01:51,710 So we can go ahead and define those two parameters. 32 00:01:51,710 --> 00:01:58,040 I'll just call this first parameter input object, and then we can call the second boolean just processed. 33 00:01:58,690 --> 00:02:03,490 And this input object has several different properties that tell us some more information about this 34 00:02:03,490 --> 00:02:05,770 particular input that has began. 35 00:02:05,800 --> 00:02:10,630 For example, if there's a particular key that the player has pressed down on their keyboard, this 36 00:02:10,630 --> 00:02:16,630 property will tell us that we can get the type of the input, whether it's a mouse, a gamepad, if 37 00:02:16,630 --> 00:02:18,520 it's a touch on the screen, and so on. 38 00:02:18,520 --> 00:02:22,060 And then we can also get the state of the user input. 39 00:02:22,300 --> 00:02:28,210 And there's a few other properties like the delta and a position if applicable. 40 00:02:28,210 --> 00:02:32,800 So what we can do here is we can print out whether or not this input was processed. 41 00:02:32,800 --> 00:02:37,510 So I can just print something like was processed by UI. 42 00:02:37,510 --> 00:02:39,460 And then I'll just pass that process. 43 00:02:39,460 --> 00:02:40,180 Boolean. 44 00:02:41,120 --> 00:02:43,760 And then we can check some other things with this input object. 45 00:02:43,760 --> 00:02:51,170 For example, we can check if the input object dot user input type is let's say equal to the enum dot 46 00:02:51,170 --> 00:02:52,970 user input type. 47 00:02:52,970 --> 00:02:55,790 Let's say we want to listen to a left mouse click. 48 00:02:55,790 --> 00:02:58,820 So we can call that mouse button one. 49 00:02:59,720 --> 00:03:05,870 So if this input that has begun is equal to the left mouse button, then that means we know that the 50 00:03:05,870 --> 00:03:07,640 left mouse button is being held down. 51 00:03:07,640 --> 00:03:11,630 So we can put a print statement like mouse was held down. 52 00:03:12,290 --> 00:03:14,810 And of course we can continue on checking for different input. 53 00:03:14,810 --> 00:03:18,320 Maybe, for example, we want to check for a particular key that was pressed. 54 00:03:18,320 --> 00:03:20,150 Then we can check in our input object. 55 00:03:20,150 --> 00:03:25,460 If the key code is equal to, let's say the enum dot key code. 56 00:03:25,460 --> 00:03:28,040 And let's check maybe for the letter e. 57 00:03:29,090 --> 00:03:34,280 So if the key code is E, that means the player has the letter E being pressed down on their keyboard 58 00:03:34,280 --> 00:03:38,930 so we can print something like key E was held down. 59 00:03:39,670 --> 00:03:41,950 And this is for the input began event. 60 00:03:41,950 --> 00:03:48,790 So any time input begins, whether that's the player beginning to press down their mouse button or press 61 00:03:48,790 --> 00:03:53,860 down a key on their keyboard, or if it's a mobile player beginning to touch their screen, this is 62 00:03:53,860 --> 00:03:55,420 the event that's going to fire. 63 00:03:55,420 --> 00:03:59,560 And then we also have inside of the user input service input ended. 64 00:03:59,560 --> 00:04:06,220 And this is going to fire when it says right here when a user stops interacting with their device. 65 00:04:06,220 --> 00:04:09,550 So we can go ahead and connect a function to this as well. 66 00:04:12,560 --> 00:04:15,920 And this function is going to be passed the exact same arguments. 67 00:04:15,920 --> 00:04:18,410 So I'm actually just going to copy all of this. 68 00:04:18,410 --> 00:04:20,750 And then we can just paste that here. 69 00:04:21,110 --> 00:04:25,730 And again we can print whether or not this input ended event was processed by UI. 70 00:04:25,760 --> 00:04:32,060 We can check to see if it was the mouse button, one that got released or the input ended, and we can 71 00:04:32,060 --> 00:04:32,720 go ahead and change. 72 00:04:32,720 --> 00:04:36,080 The statement to mouse was released. 73 00:04:36,500 --> 00:04:40,460 And then the same thing goes for the E button on our keyboard. 74 00:04:40,460 --> 00:04:43,370 We can say key E was released. 75 00:04:44,250 --> 00:04:46,860 So now we can go ahead and playtest our game. 76 00:04:47,890 --> 00:04:52,600 And as you can see, we already got a print statement inside of our console, which is the input began 77 00:04:52,600 --> 00:04:53,200 event. 78 00:04:53,200 --> 00:04:58,810 And that's because as you can see, when my mouse enters and leaves, you can see this event keeps getting 79 00:04:58,810 --> 00:04:59,140 fired. 80 00:04:59,140 --> 00:05:05,350 And that's because it's detecting this new input where the game is going back and focus because my mouse 81 00:05:05,350 --> 00:05:07,270 is inside of the viewport. 82 00:05:07,270 --> 00:05:11,410 And then when my mouse leaves the viewport, as you can see, this event keeps firing. 83 00:05:11,410 --> 00:05:17,710 So that's one way you could use this event, is to detect whether or not the player has entered or left 84 00:05:17,710 --> 00:05:22,360 the viewport with their mouse, but some of the other events I want to go ahead and take a look at is, 85 00:05:22,360 --> 00:05:23,410 for example, clicking. 86 00:05:23,410 --> 00:05:29,650 So if I hold down my left mouse button, as you can see, we got the print statement, mouse was held 87 00:05:29,650 --> 00:05:30,160 down. 88 00:05:30,160 --> 00:05:33,640 And then of course I can click other buttons on my mouse. 89 00:05:33,640 --> 00:05:38,650 For example, I'm clicking a right click right now and as you can see that event keeps firing. 90 00:05:38,650 --> 00:05:40,900 So when I hold it down, it gets printed once. 91 00:05:40,900 --> 00:05:46,240 And then when I release it, it gets printed again because the input began and the input ended. 92 00:05:46,240 --> 00:05:51,610 Event is firing, and then I can go ahead and release my finger off of my left mouse button. 93 00:05:51,610 --> 00:05:54,760 And as you can see, we get mouse was released. 94 00:05:54,940 --> 00:06:00,430 I can go ahead and do the exact same thing for the letter E on my keyboard if I press other keys on 95 00:06:00,430 --> 00:06:01,420 my keyboard. 96 00:06:02,170 --> 00:06:07,480 As you can see, the input began and the input ended event are firing each time a key is pressed on 97 00:06:07,480 --> 00:06:08,140 my keyboard. 98 00:06:08,140 --> 00:06:11,500 So that's why we're getting so many of these print statements in the console. 99 00:06:11,500 --> 00:06:17,260 But if I go ahead and press down E on my keyboard, as you can see, key E was held down and then if 100 00:06:17,260 --> 00:06:19,090 I release it, there we go. 101 00:06:19,090 --> 00:06:21,040 Key E was released. 102 00:06:21,310 --> 00:06:27,070 So the user input service allows us to observe any type of input, whether it's on our keyboard or if 103 00:06:27,070 --> 00:06:28,390 it's with our mouse. 104 00:06:28,390 --> 00:06:33,160 We can also detect touches in our world from a mobile device as well. 105 00:06:33,160 --> 00:06:35,590 So let's go ahead and take a real quick look at how to do that. 106 00:06:36,070 --> 00:06:40,900 For example, in the user input service there is an event such as touch tap. 107 00:06:40,900 --> 00:06:45,970 And if you wanted to detect a tap in the world, then we also have an event for that which is touch 108 00:06:45,970 --> 00:06:46,810 tap in world. 109 00:06:46,810 --> 00:06:48,640 So let's go ahead and check that one out real quick. 110 00:06:48,640 --> 00:06:51,130 We can go ahead and connect a function to this. 111 00:06:51,130 --> 00:06:56,530 And this time we're going to get past two arguments, which is a position where we tapped on a screen, 112 00:06:56,530 --> 00:07:01,180 and the same boolean that tells us whether or not it was processed by UI. 113 00:07:01,270 --> 00:07:03,550 So we can go ahead and get this position. 114 00:07:03,550 --> 00:07:05,560 I'll just call it screen position. 115 00:07:06,230 --> 00:07:10,280 And then we'll just call it processed for the second argument. 116 00:07:10,910 --> 00:07:16,430 And then we can go ahead and print out in our console was processed by UI. 117 00:07:17,480 --> 00:07:20,270 And then we'll just pass our process variable here. 118 00:07:20,930 --> 00:07:26,990 And then we can print out something like uh screen tap at position. 119 00:07:26,990 --> 00:07:29,690 And then we'll just pass our screen position. 120 00:07:29,690 --> 00:07:33,320 Now I'm just going to comment out these other events. 121 00:07:33,320 --> 00:07:36,950 So that way we don't get a whole bunch of these print statements flooding our console. 122 00:07:37,250 --> 00:07:39,830 And then what we can do is we can go to our test tab. 123 00:07:40,470 --> 00:07:44,190 And we can go ahead and do some device testing emulation. 124 00:07:44,190 --> 00:07:46,200 So we're going to change this to device. 125 00:07:47,010 --> 00:07:52,260 Uh, I'll just have the iPad selected and then we can go ahead and play test our game. 126 00:07:52,650 --> 00:07:54,210 And here we are inside of our game. 127 00:07:54,210 --> 00:08:00,600 So if I go ahead and click with my mouse because this is emulated, it's going to fire that touch tap 128 00:08:00,600 --> 00:08:01,290 in world event. 129 00:08:01,290 --> 00:08:03,570 So if I click there we go. 130 00:08:03,570 --> 00:08:08,520 We get our screen tap at position 720 and 367. 131 00:08:08,520 --> 00:08:13,170 So that's defining the exact point where we tapped on our screen in pixels. 132 00:08:13,680 --> 00:08:15,360 So it appears studio is freaking out. 133 00:08:15,360 --> 00:08:17,550 I'm not sure why my mouse is stuck, but that's fine. 134 00:08:17,550 --> 00:08:22,140 As you can see, our event is firing for every time we click on our screen. 135 00:08:22,980 --> 00:08:28,140 Let me see if I can try to fix this and maybe tap somewhere else on the screen real quick. 136 00:08:28,920 --> 00:08:29,370 Okay. 137 00:08:29,370 --> 00:08:31,080 I got my mouse freed up. 138 00:08:31,080 --> 00:08:34,350 If I go ahead and tap something like maybe this button right here. 139 00:08:36,430 --> 00:08:39,910 As you can see, we're getting that same event fired. 140 00:08:39,910 --> 00:08:45,820 But this time the Boolean for that process, Boolean is telling us that it was processed by UI. 141 00:08:45,820 --> 00:08:48,340 And that's because we're clicking the jump button. 142 00:08:48,700 --> 00:08:53,020 But once we stop clicking the jump button and we just click in our world, as you can see, now we're 143 00:08:53,020 --> 00:08:59,560 getting false printed into the console because it's not being processed by any UI on the screen. 144 00:08:59,560 --> 00:09:04,660 For example, if I click my username up here, as you can see that boolean changes to true. 145 00:09:04,990 --> 00:09:07,420 Or if I click up here, that's also true. 146 00:09:07,420 --> 00:09:12,850 And then when I just start clicking randomly inside of the world, that boolean gets set to false. 147 00:09:13,580 --> 00:09:20,120 So the user input service is incredibly useful for listening to specific user inputs on a player's device. 148 00:09:20,120 --> 00:09:25,970 However, there is another service in the Roblox API that makes listening to input extremely simple 149 00:09:25,970 --> 00:09:31,670 and even allows us to support multiple different devices simultaneously without needing to write extra 150 00:09:31,670 --> 00:09:36,770 code to listen to each of those specific devices, like phones, computers, or consoles. 151 00:09:36,770 --> 00:09:42,230 This service is called the Context Action Service, and we'll be taking a look at it in the next lecture. 152 00:09:42,470 --> 00:09:43,370 See you there.